import { defineStore } from "pinia";
import { ref, reactive } from 'vue'
import { reportingInfoPagePost,reportingInfoManualElevatePost } from "@/api/businessTraffic"
import { ElMessage } from "element-plus";
import { useEnumStore } from "@/store/enumeration";
import useInstance from '@/hooks/useInstance';

export const reportingListStore = defineStore("reportingListStoreId", () => {
    const { reportType, reportStatus, reportProductType } = useEnumStore()
    const { global } = useInstance();

    const detailData = ref([
        {
            type: "text",
            prop: "supplierType",
            name: "供应商类型",
            value: null
        }
    ])
    const tableProp = reactive(
        {
            name: "supplierList",
            propsList: [

                // {
                //     prop: 'channelKey',
                //     label: '渠道Key',
                //     type: 'copy',
                //     minWidth: '200px',
                //     hidden: true,
                // },
                {
                    prop: 'reportingPlatformName',
                    label: '上报平台名称',
                    type: 'text',
                    minWidth: '100px',
                    hidden: true,
                },
                {
                    prop: 'channelCode',
                    label: '渠道编码',
                    type: 'text',
                    minWidth: '80px',
                    hidden: true,
                },
                // {
                //     prop: 'channelName',
                //     label: '渠道名称',
                //     type: 'copy',
                //     minWidth: '200px',
                //     hidden: true,
                // },
                {
                    prop: 'orderNo',
                    label: '订单号',
                    type: 'text',
                    minWidth: '260px',
                    hidden: true,
                },
                {
                    prop: 'reportingSerialNo',
                    label: '上报流水单号',
                    type: 'text',
                    minWidth: '260px',
                    hidden: true,
                },

                // {
                //     prop: 'reportingPlatformCode',
                //     label: '上报平台编码',
                //     type: 'copy',
                //     minWidth: '200px',
                //     hidden: true,
                // },
                {
                    prop: 'data',
                    label: '上报数据',
                    type: 'copy',
                    minWidth: '200px',
                    hidden: true,
                },
                {
                    prop: 'enableReporting',
                    label: '是否上报',
                    type: 'text',
                    minWidth: '70px',
                    hidden: true,
                },
                {
                    prop: 'reportingType',
                    label: '上报类型',
                    type: 'text',
                    minWidth: '80px',
                    hidden: true,
                },
                {
                    prop: 'reportingStatus',
                    label: '上报状态',
                    type: 'text',
                    minWidth: '100px',
                    hidden: true,
                },
                {
                    prop: 'dataType',
                    label: '数据类型',
                    type: 'text',
                    minWidth: '100px',
                    hidden: true,
                },

                {
                    prop: 'environmentName',
                    label: '环境名称',
                    type: 'text',
                    minWidth: '140px',
                    hidden: true,
                },
                {
                    prop: 'scenarioName',
                    label: '场景名称',
                    type: 'text',
                    minWidth: '140px',
                    hidden: true,
                },
                {
                    type: 'handle',
                    label: '操作',
                    minWidth: '150',
                    btnType: ' link',
                    slotName: 'handle',
                    hidden: true,
                }
            ]
        })
    const listSearch = reactive({
        orders: ["created_time desc"],
        condition: {
            channelCode: null, // 渠道编码
            orderNo: null, //订单号
            reportingSerialNo: null, //上报流水单号

            enableReporting: null, //是否开启上报 参考枚举: YesOrNoEnum
            reportingType: null, //上报类型 参考枚举: ReportingTypeEnum

            reportingPlatformCode: null, //上报平台编码
            reportingStatus: null, //上报状态 参考枚举：ReportingStatusEnum
            environmentCode: null, //环境编码
            scenarioCode: null, //场景编码
            dataType: null, // 数据类型 参考枚举：DataTypeEnum
        },
        pageSize: 20,
        pageNum: 1
    });
    const pageParams = reactive({
        tableHeight: 400,
        total: 0,
        loadIng: false,
        tableData: [],

    })
    const getData1 = async (type: string) => {

        pageParams.loadIng = true
        if (type == 'reset') {
            listSearch.pageNum = 1
            listSearch.pageSize = 20
            Object.keys(listSearch.condition).forEach((it: any) => {
                // @ts-ignore
                listSearch.condition[it] = null
            })
        }
        try {
            const res = await reportingInfoPagePost(listSearch)
            if (res.code == 20000) {
                pageParams.tableData = res.data.records && res.data.records.map((item: any) => {
                    return {
                        ...item,
                        environmentName: item.environmentCode + ' - ' + item.environmentName,
                        scenarioName: item.scenarioCode + ' - ' + item.scenarioName,
                        enableReporting: item.enableReporting == '1' ? '是' : '否',
                        reportingType: item.reportingType == '1' ? '自动上报' : '手动上报',
                        reportingStatus: reportStatus.filter(it => it.id == item.reportingStatus)[0]?.value,
                        dataType: reportProductType[item.dataType - 1]?.value

                    }
                })
                pageParams.total = res.data.total
                pageParams.loadIng = false
            }
        } catch (error) {
            pageParams.loadIng = false
            pageParams.tableData = []
            pageParams.total = 0
        }

    }
    const reportBtn = async (row: any) => {
        // 怎么确定 操作 
        const flag = await global.$myconfirm(`是否确定上报该订单号？  ${row.orderNo}`)
        if (flag) {
            const res = await reportingInfoManualElevatePost({ id: row.id })
            console.log(res.data);
            if (res.code == 20000) {
                ElMessage({
                    type: "success",
                    message: "数据上报成功"
                })
                getData1('search')
            } else {
                ElMessage({
                    type: "error",
                    message: "数据上报失败"
                })
            }

        }
    }



    return {
        detailData, tableProp, listSearch, pageParams, getData1, reportBtn
    }
})